<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------config.php


//目录树
function tree($data,$type='',$name='title',$child='id',$parent='pid'){
	require_once('extra/Tree.php');

	$TreeData = new TreeData();
	if($type=='tree'){
		$data = $TreeData->tree($data,$name,$child,$parent);
	}elseif($type='level'){
		$data = $TreeData->channelLevel($data,0,'&nbsp;',$child);
	}
	return $data;
}

// 获取当前会员所登录有权限的菜单
function rule_tree($data){
	require_once('extra/Tree.php');
	$auth = new \think\Auth;
	$TreeData = new TreeData();

	$Newdata = $TreeData->channelLevel($data,0,'&nbsp;','id');

	foreach($Newdata as $k => $v){
		if($auth->check($v['mca'], session('uid'))){
			foreach ($v['_data'] as $m => $n) {
				if(!$auth->check($n['mca'],session('uid'))){
					unset($Newdata[$k]['_data'][$m]);
				}
			}
		}else{
			// 删除无权限的菜单
			// unset($Newdata[$k]);
		}
	}
	return $Newdata;
}

/**
 * 自定义返回json格式
 * @param $msg string 消息
 * @param $flag mixed 0 成功 1 警告 2 异常 默认为警告错误
 * @param $data array 0 json中的返回参数列表
 * @return array
 */
/**
 * 接口响应方法
 */
function res($msg, $flag="warning", $data=null, $callback = null){
	switch ($flag) {
		case 1:
			$flag = 'success';  //成功
			break;
		case 2:
			$flag = 'error';  //注销
			break;
	}
	$response['flag'] = $flag;
	$response['message'] = $msg;
	if (null != $data) {
		$response['data'] = $data;
	}
	@$callback = input('get.callback');
	if(empty($callback)){
		echo json_encode($response, JSON_UNESCAPED_UNICODE);
	}else{
		$callback = input('get.callback');
		echo $callback.'('.json_encode($response, JSON_UNESCAPED_UNICODE).')';
	}
	exit;
}
function getResData($flag = '', $message = '',$data='')
{
	$response = array('flag' => $flag,'message' => $message,'data' => $data);
	echo json_encode($response);exit;
}
// +----------------------------------------------------------------------
// | 以下为路由访问规则
// +----------------------------------------------------------------------
/**
* 模拟alert 重定向某个页面
*/
function jsBack($msg, $reUrl = null)
{
	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
	if(null != $reUrl){
		if(null != $msg){
			echo "<script>alert('".$msg."')</script>";
		}
		echo "<script>location.href='$reUrl'</script>";
	}else{
		echo "<script>history.go(-1)</script>";
	}
	if(null != $msg){
		echo "<script>alert('".$msg."')</script>";
	}
    exit;
}
//api路由
// use think\Route;

// //post方式路由
// //测试成功之后在这里删除
// Route::get([
// 		'api/sign/up'  =>  'api/sign/up',
// 		'api/sign/in'  =>  'api/sign/in',
// 		'api/sign/resetPass'  =>  'api/sign/resetPass',
// 		'/api/upload/pic'  =>  '/api/upload/pic',


// ]);
// //这里是需保留的路由
// Route::post([
// 		'api/sign/up'  =>  'api/sign/up',
// 		'api/sign/in'  =>  'api/sign/in',
// 		'api/sign/resetPass'  =>  'api/sign/resetPass',
// 		'api/upload/pic'  =>  'api/upload/pic',
// ]);


// //get方式获取的路由
// Route::get([
// 		'api/sign/out'  =>  'api/sign/out',
//         'api/video/items'  =>  'api/video/items',
// ]);
